﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using static CatUtils.Utils.Currency.PrintLog;

namespace CatUtilsDll.Utils.Currency.WebUtils
{/// <summary>
 /// 网络请求帮助
 /// </summary>
    public partial class WebHelper
    {
        #region 下载文件名辅助方法

        /// <summary>
        /// 创建一个文件名路径
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="FloderPath"></param>
        /// <param name="AutoShort"></param>
        /// <returns></returns>
        public static string CreateFilePath(string RequestUrl, string FloderPath, bool AutoShort = true)
        {
            if (!FloderHelper.FloderExits(FloderPath, true))
                return "";
            string FilePath = "";
            try
            {
                string FileName = FileNameHelper.GetFileName(RequestUrl);
                if (FileName == "")
                    return "";
                if (AutoShort && FileName.Length > 190)//文件名过长的情况进行溢出截断
                {
                    FileName = TextHelper.Substring(FileNameHelper.GetFileNameWithoutExtension(RequestUrl), 0, 190) + FileNameHelper.GetFileType(RequestUrl);
                }

                FilePath = FloderPath + FileName;
            }
            catch (Exception ex)
            {
                Log(ex);
            }

            return FilePath;
        }

        #endregion 下载文件名辅助方法

        #region 同步下载文件方法

        /// <summary>
        /// 下载文件列表
        /// </summary>
        /// <param name="FileList"></param>
        /// <param name="FloderPath"></param>
        /// <param name="headers"></param>
        public static void DownloadFileListToFloder(List<string> FileList, string FloderPath, WebHeaderCollection headers)
        {
            int CompltedCount = 0;
            foreach (string FileUrl in FileList)
            {
                new PrintLog().PercentLog(FileList.Count, ++CompltedCount);
                Console.WriteLine("下载完成" + FileNameHelper.GetFileName(DownloadToFloder(FileUrl, FloderPath, headers)));
            }
        }

        /// <summary>
        /// 文件下载辅助
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="FloderPath"></param>
        /// <returns></returns>
        public static string DownloadToFloder(string RequestUrl, string FloderPath)
        {
            return DownloadToFloder(RequestUrl, FloderPath, PcWebHeader());
        }

        /// <summary>
        /// 文件下载辅助
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="FloderPath"></param>
        /// <param name="headers"></param>
        /// <returns></returns>
        public static string DownloadToFloder(string RequestUrl, string FloderPath, WebHeaderCollection headers)
        {
            string FilePath = CreateFilePath(RequestUrl, FloderPath, true);
            return DownloadToFile(RequestUrl, FilePath, headers);
        }

        /// <summary>
        /// 下载文件辅助
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static string DownloadToFile(string RequestUrl, string FilePath)
        {
            return DownloadToFile(RequestUrl, FilePath, WebHelper.PcWebHeader());
        }

        /// <summary>
        /// 下载文件辅助
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="FilePath"></param>
        /// <param name="headers"></param>
        /// <returns></returns>
        public static string DownloadToFile(string RequestUrl, string FilePath, WebHeaderCollection headers)
        {
            using (WebClient DownloadWebClient = new WebClient())
            {
                DownloadWebClient.Headers = headers;

                try
                {
                    DownloadWebClient.DownloadFile(new Uri(RequestUrl), FilePath);
                }
                catch (Exception ex)
                {
                    E($"下载链接：{RequestUrl}出错。");
                    Log(ex);
                    FileHelper.FileDelete(FilePath);
                }
            }
            return FilePath;
        }

        /// <summary>
        ///  文件下载  返回文件的byte流
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <returns></returns>
        public static byte[] ByteDownload(string RequestUrl)
        {
            return ByteDownload(RequestUrl, WebHelper.PcWebHeader());
        }

        /// <summary>
        /// 文件下载  返回文件的byte流
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="headers"></param>
        /// <returns></returns>
        public static byte[] ByteDownload(string RequestUrl, WebHeaderCollection headers)
        {
            byte[] ImgData = null;
            using (WebClient DownloadWebClient = new WebClient())
            {
                DownloadWebClient.Headers = headers;
                try
                {
                    ImgData = DownloadWebClient.DownloadData(new Uri(RequestUrl));
                }
                catch (Exception ex)
                {
                    Log(ex);
                }
            }
            return ImgData;
        }

        #endregion 同步下载文件方法

        #region 异步下载文件方法

        /// <summary>
        /// 下载文件辅助
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="FilePath"></param>
        /// <param name="DownStringCompletedCallBack">异步下载结束事件</param>
        /// <param name="ProgressCountCallBack">异步下载进度事件</param>
        /// <returns></returns>
        public static string DownloadToFileAsync(string RequestUrl, string FilePath, AsyncCompletedEventHandler DownStringCompletedCallBack, DownloadProgressChangedEventHandler ProgressCountCallBack)
        {
            return DownloadToFileAsync(RequestUrl, FilePath, WebHelper.PcWebHeader(), DownStringCompletedCallBack, ProgressCountCallBack);
        }

        /// <summary>
        /// 异步下载文件辅助
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="FilePath"></param>
        /// <param name="headers"></param>
        /// <param name="DownStringCompletedCallBack">异步下载结束事件</param>
        /// <param name="ProgressCountCallBack">异步下载进度事件</param>
        /// <returns></returns>
        public static string DownloadToFileAsync(string RequestUrl, string FilePath, WebHeaderCollection headers,
            AsyncCompletedEventHandler DownStringCompletedCallBack, DownloadProgressChangedEventHandler ProgressCountCallBack)
        {
            using (WebClient DownloadWebClient = new WebClient())
            {
                DownloadWebClient.Headers = headers;

                DownloadWebClient.DownloadFileCompleted += DownStringCompletedCallBack;

                DownloadWebClient.DownloadProgressChanged += ProgressCountCallBack;

                try
                {
                    DownloadWebClient.DownloadFileAsync(new Uri(RequestUrl), FilePath);
                }
                catch (Exception ex)
                {
                    Log(ex);
                    return "";
                }
            }
            return FilePath;
        }

        #endregion 异步下载文件方法
    }
}